home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_09 / letters / zigler.c < prev   
Text File  |  1994-08-07  |  3KB  |  94 lines

  1.   typedef struct
  2.       {
  3.       int      cnt         ;           /* count nodes to root           */
  4.       DWORD    path        ;           /* bit-encoded path to root      */
  5.       }        HPATH       ;
  6.  
  7. /**** Module Semiglobals ****/
  8.  
  9. static DWORD      Cnt[512] ,           /* Huffman tree node counts      */
  10.                   OutSize  ,           /* track size of compressed 
  11. file */
  12.                   ByteCtr  ;           /* length of input file          */
  13. static char       Out8     ;           /* code byte to compressed 
  14. file  */
  15. static int        Cnt8     ;           /* code bit counter              */
  16. static HPATH   *  Hp       ;           /* ptr to HPATH base             */
  17. HTREE          *  Ht       ;           /* ptr to Huffman tree base      */
  18.  
  19. static int pascal encode( void )
  20.   {
  21.    register int      c           ,     /* chars from input file         */
  22.                      h           ;     /* follows path through tree     */
  23.    int               ncnt        ,     /* count nodes to root           */
  24.                      child       ;     /* child of current node         */
  25.    HPATH          *  php         ;     /* pointer to HPATH array        */
  26.    DWORD             acc   = 0L  ;     /* accumulator for code bits     */
  27.  
  28.    for ( c = 0 ; c < 256 ; c++ )       /* get Huffman codes             */
  29.       {
  30.       if ( Cnt[c] > 0 )                /* if char found in input        */
  31.          {
  32.          php   = Hp + c;               /* php->Hp[c]                    */
  33.          h     = c;
  34.          ncnt  = 0;
  35.          acc   = 0L;
  36.          do
  37.             {
  38.             ncnt++;
  39.             acc <<= 1;
  40.             child = h;                    /* child = current node       */
  41.             h = Ht[h].parent;             /* go to parent node          */
  42.             if ( child == Ht[h].left )    /* "look back" at child node  */
  43.                (int)acc |= 1;             /* encode left child node     */
  44.             }
  45.          while( Ht[h].parent != -1 );
  46.          php-cnt    = ncnt;              /* save node count            */
  47.          php-path   = acc;               /* save encoded path          */
  48.          }
  49.       }
  50.    while ( (c = getc(fi)) != EOF && OutSize < ByteCtr )
  51.       {
  52.       php   = Hp + c;
  53.       ncnt  = php-cnt;
  54.       acc   = php-path;
  55.       while ( ncnt-- )
  56.          {
  57.          emit( (int)acc & 1 );
  58.          acc >>= 1;
  59.          }
  60.       }                                /* while(getc())                 */
  61.    if ( --BytesFree  0 )             /* if out of disk space          */
  62.       longjmp( JumpBuf, ErrWrite );
  63.    if ( ++OutSize < ByteCtr )
  64.       {
  65.       OutBuffer[pBuf++] = Out8 << ( 8 - Cnt8 );       /* pad last 
  66. byte  */
  67.       _write( OutHandle, OutBuffer, pBuf );
  68.       }
  69.    return ( OutSize >= ByteCtr );
  70.    }                                   /**** encode()                ****/
  71.  
  72. static void pascal emit( register int bit )
  73.    {
  74.  
  75.    if ( Cnt8 == 8 )
  76.       {
  77.       if ( --BytesFree <= 0 )                   /* if out of disk space */
  78.          longjmp( JumpBuf, ErrWrite );
  79.       else if ( pBuf >= OUTBUFFSIZE )           /* if buffer full       */
  80.          {
  81.          _write( OutHandle, OutBuffer, pBuf );  /*  write to output     */
  82.          pBuf = 0;
  83.          }
  84.       OutBuffer[pBuf++] = Out8;
  85.       OutSize++;
  86.       Cnt8 = 0;
  87.       }
  88.    Out8 = ( Out8 << 1 ) | bit;
  89.    Cnt8++;
  90.    }
  91.  
  92. /* End of File */ 
  93.  
  94.